import java.net.InetAddress;

public class InetAddressTest {

    public static void main(String[] args) {
        try {
            // 获取本地主机的InetAddress对象
            InetAddress localAddress = InetAddress.getLocalHost();
            System.out.println(localAddress);  // 直接打印InetAddress对象

            // 获取字符串格式的原始IP地址
            System.out.println("本机的IP地址是：" + localAddress.getHostAddress());

            // 获取本机的所有ip地址
            InetAddress[] localhost = InetAddress.getAllByName("localhost");
            for (int i=0;i<localhost.length;i++){
                System.out.println("本机的ip是：" + localhost[i].getHostAddress());
            }

            // 获取给定主机名的InetAddress对象
            InetAddress baiDuAddress = InetAddress.getByName("www.baidu.com");
            System.out.println("百度的IP地址是：" + baiDuAddress.getHostAddress());

            // 判断限定时间内是否可以访问指定的IP地址
            System.out.println("5秒内是否可以访问百度网站：" + baiDuAddress.isReachable(500));

            // 获取本地IP地址的主机名
            System.out.println("baidu的主机名为：" + baiDuAddress.getHostName());
            System.out.println("本机的主机名为：" + localAddress.getHostName());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}


